package com.css.zfzx.fwsjcj.modules.structure.verify;

import cn.afterturn.easypoi.excel.entity.result.ExcelVerifyHandlerResult;
import cn.afterturn.easypoi.handler.inter.IExcelVerifyHandler;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author
 * @program
 * @description
 * @create 2022/2/14 14:42
 **/
public class FactoryStructureCheck implements IExcelVerifyHandler {
    /**
     * 错误信息
     */
    private StringBuilder msg;
    /**
     * 省市区地区map
     */
    private HashMap<String, List> map;


    public FactoryStructureCheck(HashMap map) {
        this.map = map;
    }

    public FactoryStructureCheck() {
    }

    @Override
    public ExcelVerifyHandlerResult verifyHandler(Object o) {
        // 默认为true
        ExcelVerifyHandlerResult result = new ExcelVerifyHandlerResult(true);
        msg = new StringBuilder();
        boolean hasMsg = isHasMsg(o);
        if (hasMsg) {
            result.setSuccess(false);
            result.setMsg(msg.toString());
        }
        return result;
    }

    /**
     * 是否有错误消息
     */
    private boolean isHasMsg(Object o) {
        if (isFieldHasMsg(o)) {
            msg.append("填写错误");
            return true;
        }
        return false;
    }

    /**
     * 校验字段是否市下拉列表中的字典值
     */
    private boolean isFieldHasMsg(Object o) {
        boolean flag = false;
        // 所属省份
        String province = getFieldValueByFieldName("belongtoProvince", o);
        if (province != null && !map.containsKey(province)) {
            msg.append("所属省份(填写全名称,检查省市县关系),");
            flag = true;
        } else {
            String city = getFieldValueByFieldName("belongtoCity", o);
            // 所属地级市
            if (!"北京市".equals(province) && !"天津市".equals(province) && !"上海市".equals(province) && !"重庆市".equals(province)) {
                List<String> cities = map.get(province);
                if (city != null && cities != null && !cities.contains(city)) {
                    msg.append("所属地级市(填写全名称,省市关系不匹配),");
                    flag = true;
                } else {
                    // 所属区县
                    String area = getFieldValueByFieldName("belongtoArea", o);
                    List<String> areas = map.get(city);
                    if (area != null && areas != null && !areas.contains(area)) {
                        msg.append("所属区县(填写全名称,市县关系不匹配),");
                        flag = true;
                    }
                }
            }
            // 所属地级市为直辖市
            else {
                if (city != null && !province.equals(city)) {
                    msg.append("所属地级市(填写全名称,直辖市应与省份同名)");
                    flag = true;
                } else {
                    // 所属区县
                    String area = getFieldValueByFieldName("area", o);
                    List<String> areas = map.get(city);
                    if (area != null && areas != null && !areas.contains(area)) {
                        msg.append("所属区县(填写全名称,市县关系不匹配)");
                        flag = true;
                    }
                }
            }
        }

        // 建造年代(下拉选项)
        String buildingAgeField = getFieldValueByFieldName("buildingAge", o);
        Map buildingAge=new HashMap<>();
        buildingAge.put("70s以前","");
        buildingAge.put("70s","");
        buildingAge.put("80s","");
        buildingAge.put("90s","");
        buildingAge.put("00s","");
        buildingAge.put("10s","");
        boolean buildingAgeBoo = buildingAge.containsKey(buildingAgeField);
        if (buildingAgeField != null && !buildingAgeBoo) {
            msg.append("建造年代(下拉选项),");
            flag = true;
        }

        // 场地类型(下拉选项)
        String siteTypeField = getFieldValueByFieldName("siteType", o);
        Map siteType=new HashMap<>();
        siteType.put("I类","");
        siteType.put("II类","");
        siteType.put("III类","");
        siteType.put("IV类","");
        boolean siteTypeBoo = siteType.containsKey(siteTypeField);
        if (siteTypeField != null && !siteTypeBoo) {
            msg.append("场地类型(下拉选项),");
            flag = true;
        }

        // 设防标准(下拉选项)
        String fortifyStandardField = getFieldValueByFieldName("fortifyStandard", o);
        Map fortifyStandard=new HashMap<>();
        fortifyStandard.put("0（不设防）","");
        fortifyStandard.put("6（VI度设防）","");
        fortifyStandard.put("7（VII度设防）","");
        fortifyStandard.put("8（VIII度设防）","");
        fortifyStandard.put("9（IX度设防）","");
        boolean fortifyStandardBoo = fortifyStandard.containsKey(fortifyStandardField);
        if (fortifyStandardField != null && !fortifyStandardBoo) {
            msg.append("设防标准(下拉选项),");
            flag = true;
        }

        // 结构类型(下拉选项)
        String structureTypeField = getFieldValueByFieldName("structureType", o);
        Map structureType=new HashMap<>();
        structureType.put("排架结构","");
        structureType.put("门式刚架","");
        structureType.put("框排架结构","");
        boolean structureTypeBoo = structureType.containsKey(structureTypeField);
        if (structureTypeField != null && !structureTypeBoo) {
            msg.append("结构类型(下拉选项),");
            flag = true;
        }

        // 吊车类型(下拉选项)
        String craneTypeField = getFieldValueByFieldName("craneType", o);
        Map craneType=new HashMap<>();
        craneType.put("无","");
        craneType.put("桥式吊车","");
        craneType.put("悬挂吊车","");
        craneType.put("其他","");
        boolean craneTypeBoo = craneType.containsKey(craneTypeField);
        if (craneTypeField != null && !craneTypeBoo) {
            msg.append("吊车类型(下拉选项),");
            flag = true;
        }

        // 排架柱类型(下拉选项)
        String bentFrameColTypeField = getFieldValueByFieldName("bentFrameColType", o);
        Map bentFrameColType=new HashMap<>();
        bentFrameColType.put("钢柱","");
        bentFrameColType.put("砼柱","");
        bentFrameColType.put("砖柱","");
        bentFrameColType.put("砼预制柱","");
        boolean bentFrameColTypeBoo = bentFrameColType.containsKey(bentFrameColTypeField);
        if (bentFrameColTypeField != null && !bentFrameColTypeBoo) {
            msg.append("排架柱类型(下拉选项),");
            flag = true;
        }

        // 柱混凝土强度等级(下拉选项)
        String colConcreteStrengthGradeField = getFieldValueByFieldName("colConcreteStrengthGrade", o);
        Map colConcreteStrengthGrade=new HashMap<>();
        colConcreteStrengthGrade.put("无","");
        colConcreteStrengthGrade.put("C15","");
        colConcreteStrengthGrade.put("C20","");
        colConcreteStrengthGrade.put("C25","");
        colConcreteStrengthGrade.put("C30","");
        colConcreteStrengthGrade.put("C35","");
        colConcreteStrengthGrade.put("C40","");
        colConcreteStrengthGrade.put("C45","");
        colConcreteStrengthGrade.put("C50","");
        colConcreteStrengthGrade.put("C55","");
        colConcreteStrengthGrade.put("C60","");
        colConcreteStrengthGrade.put("C65","");
        colConcreteStrengthGrade.put("C70","");
        colConcreteStrengthGrade.put("C75","");
        colConcreteStrengthGrade.put("C80","");
        colConcreteStrengthGrade.put("其他","");
        boolean colConcreteStrengthGradeBoo = colConcreteStrengthGrade.containsKey(colConcreteStrengthGradeField);
        if (colConcreteStrengthGradeField != null && !colConcreteStrengthGradeBoo) {
            msg.append("柱混凝土强度等级(下拉选项),");
            flag = true;
        }

        // 排架柱是否等高(下拉选项)
        String bentFrameColEqualField = getFieldValueByFieldName("bentFrameColEqual", o);
        Map bentFrameColEqual=new HashMap<>();
        bentFrameColEqual.put("0（不等高）","");
        bentFrameColEqual.put("1（等高）","");
        boolean bentFrameColEqualBoo = bentFrameColEqual.containsKey(bentFrameColEqualField);
        if (bentFrameColEqualField != null && !bentFrameColEqualBoo) {
            msg.append("排架柱是否等高(下拉选项),");
            flag = true;
        }

        // 柱是否变截面(下拉选项)
        String isColumnChangeSectionField = getFieldValueByFieldName("isColumnChangeSection", o);
        Map isColumnChangeSection=new HashMap<>();
        isColumnChangeSection.put("是","");
        isColumnChangeSection.put("否","");
        boolean isColumnChangeSectionBoo = isColumnChangeSection.containsKey(isColumnChangeSectionField);
        if (isColumnChangeSectionField != null && !isColumnChangeSectionBoo) {
            msg.append("柱是否变截面(下拉选项),");
            flag = true;
        }

        // 围护墙体材质(下拉选项)
        String wallMaterialField = getFieldValueByFieldName("wallMaterial", o);
        Map wallMaterial=new HashMap<>();
        wallMaterial.put("普通砖","");
        wallMaterial.put("多孔砖","");
        wallMaterial.put("砌块","");
        wallMaterial.put("其他","");
        boolean wallMaterialBoo = wallMaterial.containsKey(wallMaterialField);
        if (wallMaterialField != null && !wallMaterialBoo) {
            msg.append("围护墙体材质(下拉选项),");
            flag = true;
        }

        // 围护墙砂浆类型(下拉选项)
        String wallMortarTypeField = getFieldValueByFieldName("wallMortarType", o);
        Map wallMortarType=new HashMap<>();
        wallMortarType.put("水泥砂浆","");
        wallMortarType.put("混合砂浆","");
        wallMortarType.put("泥浆","");
        boolean wallMortarTypeBoo = wallMortarType.containsKey(wallMortarTypeField);
        if (wallMortarTypeField != null && !wallMortarTypeBoo) {
            msg.append("围护墙砂浆类型(下拉选项),");
            flag = true;
        }

        // 围护墙砂浆强度(下拉选项)
        String wallMortarStrengthGradeField = getFieldValueByFieldName("wallMortarStrengthGrade", o);
        Map wallMortarStrengthGrade=new HashMap<>();
        wallMortarStrengthGrade.put("大于等于M2.5","");
        wallMortarStrengthGrade.put("小于M2.5","");
        boolean wallMortarStrengthGradeBoo = wallMortarStrengthGrade.containsKey(wallMortarStrengthGradeField);
        if (wallMortarStrengthGradeField != null && !wallMortarStrengthGradeBoo) {
            msg.append("围护墙砂浆强度(下拉选项),");
            flag = true;
        }

        // 围护墙与柱拉结构造(下拉选项)
        String wallColField = getFieldValueByFieldName("wallCol", o);
        Map wallCol=new HashMap<>();
        wallCol.put("无连接","");
        wallCol.put("有连接","");
        wallCol.put("每隔十皮砖有筋与柱拉结","");
        wallCol.put("无法判断","");
        boolean wallColBoo = wallCol.containsKey(wallColField);
        if (wallColField != null && !wallColBoo) {
            msg.append("围护墙与柱拉结构造(下拉选项),");
            flag = true;
        }

        // 有无天窗(下拉选项)
        String isSkylightField = getFieldValueByFieldName("isSkylight", o);
        Map isSkylight=new HashMap<>();
        isSkylight.put("0（无）","");
        isSkylight.put("1（有）","");
        boolean isSkylightBoo = isSkylight.containsKey(isSkylightField);
        if (isSkylightField != null && !isSkylightBoo) {
            msg.append("有无天窗(下拉选项),");
            flag = true;
        }

        // 屋盖体系(下拉选项)
        String roofSystemField = getFieldValueByFieldName("roofSystem", o);
        Map roofSystem=new HashMap<>();
        roofSystem.put("有檩屋盖","");
        roofSystem.put("无檩屋盖","");
        boolean roofSystemBoo = roofSystem.containsKey(roofSystemField);
        if (roofSystemField != null && !roofSystemBoo) {
            msg.append("屋盖体系(下拉选项),");
            flag = true;
        }

        // 屋架形式(下拉选项)
        String roofTrussStyleField = getFieldValueByFieldName("roofTrussStyle", o);
        Map roofTrussStyle=new HashMap<>();
        roofTrussStyle.put("实腹钢梁","");
        roofTrussStyle.put("钢桁架","");
        roofTrussStyle.put("钢网架","");
        roofTrussStyle.put("木屋架","");
        roofTrussStyle.put("钢屋架","");
        boolean roofTrussStyleBoo = roofTrussStyle.containsKey(roofTrussStyleField);
        if (roofTrussStyleField != null && !roofTrussStyleBoo) {
            msg.append("屋架形式(下拉选项),");
            flag = true;
        }

        // 柱间支撑(下拉选项)
        String zjzcField = getFieldValueByFieldName("zjzc", o);
        Map zjzc=new HashMap<>();
        zjzc.put("无支撑","");
        zjzc.put("单元中部有一道柱间支撑","");
        zjzc.put("单元两端各有一道柱间支撑","");
        boolean zjzcBoo = zjzc.containsKey(zjzcField);
        if (zjzcField != null && !zjzcBoo) {
            msg.append("柱间支撑(下拉选项),");
            flag = true;
        }

        // 屋架上弦支撑(下拉选项)
        String wjsxzcField = getFieldValueByFieldName("wjsxzc", o);
        Map wjsxzc=new HashMap<>();
        wjsxzc.put("无水平支撑","");
        wjsxzc.put("厂房单元端开间设支撑","");
        wjsxzc.put("端开间及有柱间支撑的开间设支撑","");
        boolean wjsxzcBoo = wjsxzc.containsKey(wjsxzcField);
        if (wjsxzcField != null && !wjsxzcBoo) {
            msg.append("屋架上弦支撑(下拉选项),");
            flag = true;
        }

        // 屋架下弦支撑(下拉选项)
        String wjxxzcField = getFieldValueByFieldName("wjxxzc", o);
        Map wjxxzc=new HashMap<>();
        wjxxzc.put("无水平支撑","");
        wjxxzc.put("厂房单元端开间设支撑","");
        wjxxzc.put("端开间及有柱间支撑的开间设支撑","");
        boolean wjxxzcBoo = wjxxzc.containsKey(wjxxzcField);
        if (wjxxzcField != null && !wjxxzcBoo) {
            msg.append("屋架下弦支撑(下拉选项),");
            flag = true;
        }

        // 屋架跨中竖向支撑(下拉选项)
        String wjkzsxzcField = getFieldValueByFieldName("wjkzsxzc", o);
        Map wjkzsxzc=new HashMap<>();
        wjkzsxzc.put("无支撑","");
        wjkzsxzc.put("厂房单元端开间设支撑","");
        wjkzsxzc.put("端开间及有柱间支撑的开间设支撑","");
        boolean wjkzsxzcBoo = wjkzsxzc.containsKey(wjkzsxzcField);
        if (wjkzsxzcField != null && !wjkzsxzcBoo) {
            msg.append("屋架跨中竖向支撑(下拉选项),");
            flag = true;
        }

        // 屋架两端竖向支撑(下拉选项)
        String wjldsxzcField = getFieldValueByFieldName("wjldsxzc", o);
        Map wjldsxzc=new HashMap<>();
        wjldsxzc.put("无支撑","");
        wjldsxzc.put("厂房单元端开间设支撑","");
        wjldsxzc.put("端开间及有柱间支撑的开间设支撑","");
        boolean wjldsxzcBoo = wjldsxzc.containsKey(wjldsxzcField);
        if (wjldsxzcField != null && !wjldsxzcBoo) {
            msg.append("屋架两端竖向支撑(下拉选项),");
            flag = true;
        }

        // 天窗两侧竖向支撑(下拉选项)
        String tclcsxzcField = getFieldValueByFieldName("tclcsxzc", o);
        Map tclcsxzc=new HashMap<>();
        tclcsxzc.put("无支撑","");
        tclcsxzc.put("支撑间距小于18米","");
        tclcsxzc.put("支撑间距小于30米","");
        tclcsxzc.put("支撑间距小于42米","");
        tclcsxzc.put("支撑间距大于42米","");
        boolean tclcsxzcBoo = tclcsxzc.containsKey(tclcsxzcField);
        if (tclcsxzcField != null && !tclcsxzcBoo) {
            msg.append("天窗两侧竖向支撑(下拉选项),");
            flag = true;
        }

        // 女儿墙拉结构造(下拉选项)
        String parapetInstallField = getFieldValueByFieldName("parapetInstall", o);
        Map parapetInstall=new HashMap<>();
        parapetInstall.put("无女儿墙","");
        parapetInstall.put("女儿墙无拉结","");
        parapetInstall.put("女儿墙有拉结","");
        boolean parapetInstallBoo = parapetInstall.containsKey(parapetInstallField);
        if (parapetInstallField != null && !parapetInstallBoo) {
            msg.append("女儿墙拉结构造(下拉选项),");
            flag = true;
        }

        // 女儿墙高度(下拉选项)
        String parapetHeightField = getFieldValueByFieldName("parapetHeight", o);
        Map parapetHeight=new HashMap<>();
        parapetHeight.put("无女儿墙","");
        parapetHeight.put("大于500mm","");
        parapetHeight.put("小于等于500mm","");
        boolean parapetHeightBoo = parapetHeight.containsKey(parapetHeightField);
        if (parapetHeightField != null && !parapetHeightBoo) {
            msg.append("女儿墙高度(下拉选项),");
            flag = true;
        }

        // 屋架端部上弦和柱顶圈梁设置(下拉选项)
        String roofTrussFirstQuarterStigmaField = getFieldValueByFieldName("roofTrussFirstQuarterStigma", o);
        Map roofTrussFirstQuarterStigma=new HashMap<>();
        roofTrussFirstQuarterStigma.put("无圈梁","");
        roofTrussFirstQuarterStigma.put("有一道圈梁","");
        roofTrussFirstQuarterStigma.put("各有一道圈梁","");
        roofTrussFirstQuarterStigma.put("无法判断","");
        boolean roofTrussFirstQuarterStigmaBoo = roofTrussFirstQuarterStigma.containsKey(roofTrussFirstQuarterStigmaField);
        if (roofTrussFirstQuarterStigmaField != null && !roofTrussFirstQuarterStigmaBoo) {
            msg.append("屋架端部上弦和柱顶圈梁设置(下拉选项),");
            flag = true;
        }

        // 沿墙高圈梁设置(下拉选项)
        String yqgqlInstallField = getFieldValueByFieldName("yqgqlInstall", o);
        Map yqgqlInstall=new HashMap<>();
        yqgqlInstall.put("无圈梁","");
        yqgqlInstall.put("间距小于等于6米","");
        yqgqlInstall.put("间距大于6米","");
        yqgqlInstall.put("无法判断","");
        boolean yqgqlInstallBoo = yqgqlInstall.containsKey(yqgqlInstallField);
        if (yqgqlInstallField != null && !yqgqlInstallBoo) {
            msg.append("沿墙高圈梁设置(下拉选项),");
            flag = true;
        }

        // 柱现状(下拉选项)
        String zSituationField = getFieldValueByFieldName("zSituation", o);
        Map zSituation=new HashMap<>();
        zSituation.put("无开裂或损伤","");
        zSituation.put("有开裂或损伤","");
        zSituation.put("多处损伤","");
        boolean zSituationBoo = zSituation.containsKey(zSituationField);
        if (zSituationField != null && !zSituationBoo) {
            msg.append("柱现状(下拉选项),");
            flag = true;
        }

        // 屋面构件现状(下拉选项)
        String wmgjSituationField = getFieldValueByFieldName("wmgjSituation", o);
        Map wmgjSituation=new HashMap<>();
        wmgjSituation.put("无变形","");
        wmgjSituation.put("有变形","");
        wmgjSituation.put("无开裂","");
        wmgjSituation.put("有开裂","");
        boolean wmgjSituationBoo = wmgjSituation.containsKey(wmgjSituationField);
        if (wmgjSituationField != null && !wmgjSituationBoo) {
            msg.append("屋面构件现状(下拉选项),");
            flag = true;
        }

        // 填充墙现状(下拉选项)
        String fillWellSituationField = getFieldValueByFieldName("fillWellSituation", o);
        Map fillWellSituation=new HashMap<>();
        fillWellSituation.put("无明显裂缝","");
        fillWellSituation.put("有明显裂缝","");
        fillWellSituation.put("多处明显裂缝","");
        boolean fillWellSituationBoo = fillWellSituation.containsKey(fillWellSituationField);
        if (fillWellSituationField != null && !fillWellSituationBoo) {
            msg.append("填充墙现状(下拉选项),");
            flag = true;
        }

        // 基础现状(下拉选项)
        String baseSituationField = getFieldValueByFieldName("baseSituation", o);
        Map baseSituation=new HashMap<>();
        baseSituation.put("无不均匀沉陷","");
        baseSituation.put("有不均匀沉陷","");
        boolean baseSituationBoo = baseSituation.containsKey(baseSituationField);
        if (baseSituationField != null && !baseSituationBoo) {
            msg.append("基础现状(下拉选项),");
            flag = true;
        }


        //经度
        String longitudeField = getFieldValueByFieldName("longitude", o);
        if(longitudeField!=null && !"".equals(longitudeField)){
            double longitude=Double.valueOf(longitudeField);
            if(longitude>180.0 || longitude<-180.0){
                msg.append("经度,");
                flag = true;
            }
        }
        //纬度
        String latitudeField = getFieldValueByFieldName("latitude", o);
        if(!"".equals(latitudeField) && latitudeField!=null) {
            double latitude = Double.valueOf(latitudeField);
            if (latitude > 90.0 || latitude < -90.0) {
                msg.append("纬度,");
                flag = true;
            }
        }

        return flag;
    }





    /**
     * 根据属性名获取属性值
     *
     * @param fieldName 属性名
     * @param object    对象
     * @return
     */
    private String getFieldValueByFieldName(String fieldName, Object object) {
        try {
            String firstLetter = fieldName.substring(0, 1).toUpperCase();
            String getter = "get" + firstLetter + fieldName.substring(1);
            Method method = object.getClass().getMethod(getter, new Class[]{});
            Object value = method.invoke(object, new Object[]{});
            return value.toString();
        } catch (Exception e) {
            return null;
        }
    }
}
